<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :arr="list" @create="createFn"></TodoHeader>
    <TodoMain :arr="changeSel" @remove="removeFn"></TodoMain>
    <TodoFooter
      :arr="changeSel"
      @change="changeFn"
      @removeAll="removeAllFn"
    ></TodoFooter>
  </section>
</template>
<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("plist")) || [],
      getSel: "all",
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    createFn(taskName) {
      let id =
        this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 100;
      this.list.push({
        id: id,
        name: taskName,
        isDone: false,
      });
    },
    removeFn(index) {
      let id = this.list.findIndex((item) => (item.id = index));
      this.list.splice(id, 1);
    },
    changeFn(sel) {
      this.getSel = sel;
    },
    removeAllFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
  },
  computed: {
    changeSel() {
      if (this.getSel === "yes") {
        return this.list.filter((item) => item.isDone);
      } else if (this.getSel === "no") {
        return this.list.filter((item) => !item.isDone);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler() {
        localStorage.setItem("plist", JSON.stringify(this.list));
      },
      deep: true,
    },
  },
};
</script>
<style >
</style>
